package com.suyuan.controller.admin;

import com.github.pagehelper.PageInfo;
import com.suyuan.entity.CarInfo;
import com.suyuan.entity.CompanyInfo;
import com.suyuan.entity.TransitBatchInfo;
import com.suyuan.entity.TransportRouteInfor;
import com.suyuan.service.TransitBatchInfoService;
import com.suyuan.utils.JsonMessage;
import com.suyuan.web.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 *  物流批次管理控制类
 *
 */
@Controller
@RequestMapping(value = "/admin/transitBatchInfo")
public class TransitBatchInfoController {

    @Resource(name = "transitBatchInfoService")
    private TransitBatchInfoService transitBatchInfoService;

    /**
     * 跳转添加页面
     * @return
     */
    @RequestMapping(value = "/add")
    public String add(ModelMap model){
        //路线集合
        List<TransportRouteInfor> routes = transitBatchInfoService.getRoutes();
        //货车集合
        List<CarInfo> cars = transitBatchInfoService.getCars();
        //物流公司集合
        List<CompanyInfo> companies = transitBatchInfoService.getCompanies();

        model.addAttribute("routes",routes);
        model.addAttribute("cars",cars);
        model.addAttribute("companies",companies);
        return "transitBatch_add";
    }

    /**
     * 保存数据
     * @param transitBatchInfo
     * @return
     */
    @RequestMapping(value = "/save")
    @ResponseBody
    public Object save(TransitBatchInfo transitBatchInfo){
        int code = 0;
        try {
            code = transitBatchInfoService.insert(transitBatchInfo);
        } catch (Exception e) {
            code = 0;
        }
        if(code == 1){
            return JsonMessage.success("添加成功");
        }
        return JsonMessage.error("添加失败");
    }

    /**
     * 根据id删除数据
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(Integer id){
        int code = transitBatchInfoService.delete(id);
        if(code == 1){
            return JsonMessage.success("删除成功");
        }
        return JsonMessage.error("删除失败");
    }

    /**
     * 模糊查询
     * @return
     */
    @RequestMapping(value = "/list")
    public String list(String batchId , String routeId, ModelMap model, Page page){
        PageInfo info = transitBatchInfoService.inquiry(batchId,routeId,page);
        model.addAttribute("page",info);
        model.addAttribute("batchId",batchId);
        model.addAttribute("routeId",routeId);
        return "transitBatch_list";
    }

    /**
     * 批量删除
     */
    @RequestMapping(value = "/deleteByIds")
    @ResponseBody
    public Object deleteByIds(String ids){
        if(StringUtils.isBlank(ids)){
            return null;
        }
        String[] idArr;
        if(ids.length() >= 2){
            idArr = ids.split(",");
        }else{
            idArr = new String[]{ids};
        }
        for(String id : idArr){
            transitBatchInfoService.delete(Integer.parseInt(id));
        }
        return null;
    }

    /**
     * 编辑页面
     * @return
     */
    @RequestMapping(value = "/edit")
    public String edit(Integer id,ModelMap model){
        TransitBatchInfo batch= transitBatchInfoService.findById(id);
        //路线集合
        List<TransportRouteInfor> routes = transitBatchInfoService.getRoutes();
        //货车集合
        List<CarInfo> cars = transitBatchInfoService.getCars();
        //物流公司集合
        List<CompanyInfo> companies = transitBatchInfoService.getCompanies();
        model.addAttribute("routes",routes);
        model.addAttribute("cars",cars);
        model.addAttribute("companies",companies);
        model.addAttribute("batch",batch);
        return "transitBatch_edit";
    }

    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(TransitBatchInfo batchInfo){
        int code = transitBatchInfoService.update(batchInfo);
        if(code == 1){
            return JsonMessage.success("更新成功");
        }
        return JsonMessage.error("更新失败 ");
    }

}
